//
//  HomeNextHeaderView.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/6/14.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "HomeNextHeaderView.h"
#import "HomeHeaderCollectionViewCell.h"

static const CGFloat bannerHeight = 115;
static const CGFloat goodsHeight = 150;
static const CGFloat titleHeight = 30;

@interface HomeNextHeaderView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) SDCycleScrollView *sdcView;
@property (nonatomic,strong) UICollectionView *collectionView;
Strong UIView *titleView;

@end

@implementation HomeNextHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = getColor(@"f8f8f8");
        
        [self addSubview:self.sdcView];
        
        CGRect titleRect = CGRectMake(0, self.frame.size.height - titleHeight - 4 , SCREEN_WIDTH, titleHeight);
        _titleView = [[UIView alloc] initWithFrame:titleRect];
        
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 9, SCREEN_WIDTH, 12)];
        imgView.image = [UIImage imageNamed:@"home_title_line"];
        imgView.contentMode = 1;
        
        [_titleView addSubview:imgView];
        _titleView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_titleView];
    }
    return self;
}

#pragma mark - Event

- (void)menuEvent:(UITapGestureRecognizer *)sender{
    if (self.menuHandle) {
        self.menuHandle(sender.view.tag - 200);
    }
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
        return self.menuArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        HomeHeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HomeHeaderCollectionViewCell class]) forIndexPath:indexPath];
        cell.dataDictionary = self.menuArray[indexPath.row];
        return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
        if (_menuHandle) {
            _menuHandle(indexPath.row);
        }
}

#pragma mark - set & get

- (void)setBannerArray:(NSArray *)bannerArray{
    _bannerArray = bannerArray;
    NSMutableArray *array = @[].mutableCopy;
    for (NSDictionary *dic in _bannerArray) {
        [array addObject:[NSURL URLOffsetIPWithString:dic[@"ac_picture"]].absoluteString];
    }
    _sdcView.imageURLStringsGroup = array;
}

- (SDCycleScrollView *)sdcView{
    if (!_sdcView) {
        _sdcView = [[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, bannerHeight/WIDTH_5S_SCALE)];
        _sdcView.backgroundColor = getColor(mainColor);
        _sdcView.autoScrollTimeInterval = 3.0;
        _sdcView.backgroundColor = getColor(@"bfbfbf");
        _sdcView.pageControlDotSize = CGSizeMake(11, 11);
        _sdcView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _sdcView.currentPageDotImage = [UIImage imageNamed:@"homepage_banner_point"];
        _sdcView.pageDotImage = [UIImage imageNamed:@"homepage_banner_circle"];
        __block typeof(self) bSelf = self;
        _sdcView.clickItemOperationBlock = ^(NSInteger currentIndex) {
            if (bSelf.bannerHandle) {
                bSelf.bannerHandle(currentIndex);
            }
        };
        
    }
    return _sdcView;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.itemSize = CGSizeMake(SCREEN_WIDTH/5., goodsHeight/WIDTH_5S_SCALE/2.);
        CGRect rect = CGRectMake(0, self.sdcView.frame.size.height, SCREEN_WIDTH, goodsHeight/WIDTH_5S_SCALE / 2 * (self.menuArray.count / 5 + (self.menuArray.count % 5 == 0 ? + 0 : + 1)));
        _collectionView = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        [_collectionView registerClass:[HomeHeaderCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([HomeHeaderCollectionViewCell class])];
    }
    return _collectionView;
}

- (void)setMenuArray:(NSArray *)menuArray{
    _menuArray = menuArray;
//    [self.collectionView reloadData];
    [self addSubview:self.collectionView];
}

@end
